package com.xyd.Servlet;

import com.xyd.Biz.Impl.MessageBizImpl;
import com.xyd.Biz.MessageBiz;
import com.xyd.Entity.Message;
import com.xyd.Entity.User;
import com.xyd.Util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/html/manage/MessageManageServlet")
public class MessageManageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        if(op.equals("update")){
            update(req, resp);
        }else if(op.equals("delete")){
            delete(req, resp);
        }else if(op.equals("select")){
            select(req, resp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MessageBiz messageBiz = new MessageBizImpl();
        String id = req.getParameter("id");
        Message message= messageBiz.selectMsgbyId(Integer.parseInt(id));
        message.setVal_type(0);
        int flag = messageBiz.updateMsgByMsg(message);
        if(flag>0){
            resp.sendRedirect("guestbook.jsp");
        }else{
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().write("<script>alter('删除失败')</script>");
            resp.sendRedirect("guestbook.jsp");
        }
    }

    public void select(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MessageBiz messageBiz = new MessageBizImpl();
        String id = req.getParameter("id");
        Message message= messageBiz.selectMsgbyId(Integer.parseInt(id));
        JsonServlet.getJson(req,resp,message);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        MessageBiz messageBiz = new MessageBizImpl();
        String replyContent = req.getParameter("replyContent");
        String id = req.getParameter("orderId");
        Message message= messageBiz.selectMsgbyId(Integer.parseInt(id));
        message.setRespcontext(replyContent);
        int flag = messageBiz.updateMsgByMsg(message);
        if(flag>0){
            resp.sendRedirect("guestbook.jsp");
        }else{
            resp.setContentType("text/html;charset=UTF-8");
            resp.getWriter().write("<script>alter('失败')</script>");
            resp.sendRedirect("guestbook.jsp");
        }
    }
}
